package me.zhengjie.modules.system.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.persistence.*
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import me.zhengjie.common.base.BaseEntity
import me.zhengjie.common.utils.JacksonUtil
import org.hibernate.annotations.CreationTimestamp
import org.springframework.data.redis.core.RedisHash
import java.sql.Timestamp

/**
 * @author Kuki Wu
 * @date 2019-03-29
 */
@Entity
@Table(name = "sys_job")
@JsonTypeName("sysjob")
@RedisHash("sysjob")
data class Job (
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "job_id")
    @Schema(description = "ID", hidden = true)
    var id: @NotNull(groups = [Update::class]) Long? = null,

    @Schema(description = "岗位名称")
    var name: @NotBlank String? = null,

    @NotNull @Schema(description = "岗位排序")
    var jobSort: Long? = 0L,

    @Schema(description = "是否启用")
    var enabled: @NotNull Boolean? = null,
):BaseEntity(){
    annotation class Update

    companion object {
        fun fromJson(json: String): Job? {
            val obj = JacksonUtil.parseObject(json, Job::class.java)!!
            return when (obj) {
                is Job -> obj
                else -> null
            }
        }
    }

    fun copy(source: Job?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}